import { observable, action } from 'mobx';
import { WFBatchPrintIndexStore } from './batchPrintIndex';
import * as API from '../apis/batchPrint';
import { WeaForm } from 'comsMobx';
import { WeaTableNew } from 'comsMobx'
const { TableStore } = WeaTableNew;
import * as _ from 'lodash';

export class WFBatchPrintResultStore extends WFBatchPrintIndexStore{

    @observable showSearchAd = false;
    @observable tableStore = new TableStore();
    
    constructor(){
        super();
        this.copyIndexStore = this.copyIndexStore.bind(this);
        this.setShowSearchAd = this.setShowSearchAd.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.doChangePrintStatus = this.doChangePrintStatus.bind(this);
        this.reset = this.reset.bind(this);
    }

    @action
    copyIndexStore(store){
        if(_.isEmpty(store.fixedCondition)){  //F5刷新
            this.loadFixedCondition();
        }else{  //复制导航条件
            this.fixedCondition = store.fixedCondition;
            this.fixedForm.initFormFields(store.fixedCondition);
            const fixedValues = store.fixedForm && store.fixedForm.getFormDatas() || {};
            this.fixedForm.updateFields(fixedValues, true);
            //复制表单字段条件
            this.nodeOption = store.nodeOption;
            this.showDetailField = store.showDetailField;
            if(store.fieldCondition && store.fieldCondition.length > 0){
                this.fieldCondition = store.fieldCondition;
                this.workflowid = store.workflowid;
                this.fieldForm = new WeaForm();
                this.fieldForm.initFormFieldObj(store.fieldCondition);
                const fieldValues = store.fieldForm && store.fieldForm.getFormDatas() || {};
                this.fieldForm.updateFields(fieldValues, true);
            }
        }
    }

    setShowSearchAd(bool) {
        this.showSearchAd = bool;
    }

    doSearch(params = {}, clearSelected=false){
        this.setShowSearchAd(false);
        const fixedSearchParams = this.fixedForm ? this.fixedForm.getFormParams() : {};
        //表单字段条件
        let fieldSearchParams = {};
        if(this.fieldCondition.length > 0){
            let fieldConditionMap = {};
            this.fieldCondition.map(v =>{
                fieldConditionMap[v.otherParams.fieldid] = v;
            });
            let fieldids = "";
            let fieldFormParams = this.fieldForm ? this.fieldForm.getFormParams() : {};
            const reg = new RegExp("^(custom_)(\\d+)_");    //正则分组
            for(const key in fieldFormParams){
                if(reg.test(key)){      //自定义查询条件值
                    const fieldid = RegExp.$2;
                    const condition = fieldConditionMap[fieldid];
                    if(!this.showDetailField && condition.otherParams.isdetail === 1)
                        continue;
                    fieldSearchParams[key] = fieldFormParams[key];
                    if(`,${fieldids},`.indexOf(`,${fieldid},`) === -1)
                        fieldids +=  ","+fieldid;
                    //部分类型字段，置默认值
                    const htmltype = condition.otherParams.htmltype;
                    const detailtype = condition.otherParams.detailtype;
                    if((htmltype === 1 && detailtype === 1) || htmltype === 2){
                        fieldSearchParams[`custom_${fieldid}_opt`] = "3";     //包含
                    }else if(htmltype === 1 && detailtype !== 1){
                        fieldSearchParams[`custom_${fieldid}_opt`] = "2";     //大于或等于
                        fieldSearchParams[`custom_${fieldid}_opt1`] = "4";    //小于或等于
                    }else if(htmltype === 3 && (detailtype === 2 || detailtype === 19)){
                        fieldSearchParams[`custom_${fieldid}_opt`] = "2";     //大于或等于
                        fieldSearchParams[`custom_${fieldid}_opt1`] = "4";    //小于或等于
                    }else if(htmltype === 3 && condition.conditionType === "BROWSER"){
                        fieldSearchParams[`custom_${fieldid}_opt`] = "1";     //包含
                    }
                }
            }
            fieldSearchParams["fieldids"] = fieldids;
        }
        this.loading = true;
        let reqParams = {...params, ...fixedSearchParams, ...fieldSearchParams};
        const current =  this.tableStore.current;
        API.getListResult(reqParams).then(datas =>{
            this.loading = false;
            if(clearSelected)
                this.tableStore.tableUpdate({selectedRowKeys:[], selectedRowKeysAllPages:[]});
            this.hasRight = datas.hasRight;
            this.tableStore.getDatas(datas.sessionkey, current || 1, "", params.sortParams || []);
        });
    }

    doChangePrintStatus(params={}){
        this.loading = true;
        API.doChangePrintStatus(params).then(datas =>{
            this.loading = false;
            if(datas.flag)
                this.reLoad();
        });
    }

    reLoad(){
        this.doSearch({},true);
    }

    reset(){
        this.urlParams = {};
        this.showSearchAd = false;
        this.tableStore = new TableStore();
        this.resetCondition(true);
    }
}